(function(angular) {
	"use strict";
	angular.module("fs.waf").controller("cls.popup.intAccrualList.listController", listController);
	listController.$inject = [ "$scope", "$uibModalInstance", "dialogsModule", "gridService", "layoutConfig", "validatorConfig", "authService", "dateService" ];
	function listController($scope, $uibModalInstance, dialogsModule, gridService, layoutConfig, validatorConfig, authService, dateService) {
		var ctrl = this;
		ctrl.dialogsModule = dialogsModule;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.searchCriteria = ctrl.searchCriteria || {};

		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback["close"] || dialogsModule.btnCallback["close"]() !== false) {
				$uibModalInstance.dismiss();
			}
		};

		ctrl.init = function() {
			ctrl.innerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.tableConfigs.table, rowSelectionChanged);
			ctrl.innerGridOptions.data = "vm.searchCriteria.list";
			ctrl.accrTypeOptions = [ {
				"value" : "M",
				"label" : "option.intaccrual.m"
			},
			{
				"value" : "A",
				"label" : "option.intaccrual.a"
			}];
			if (!ctrl.dialogsModule.params.loanId) {
				return;
			}
			ctrl.searchCriteria = angular.merge(ctrl.searchCriteria, ctrl.dialogsModule.params);
			ctrl.search();
		};

		ctrl.search = function() {
			ctrl.searchCriteria.list = [];
			authService.search(ctrl.searchCriteria).then(function(response) {
				angular.merge(ctrl.searchCriteria, response.data);
				angular.forEach(ctrl.searchCriteria.list, function(intAccrual) {
					intAccrual.toDate = angular.copy(intAccrual.fromDate);
					intAccrual.toDate = dateService.addDays(intAccrual.toDate, intAccrual.numDays);
				});
				gridService.updateGridOptions(ctrl.searchCriteria, ctrl.innerGridOptions);
			}, angular.noop);
		};

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
		}

	}
})(angular);